﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DDD从零起步.Controllers
{
    using Domain.Service;
    using Domain.Entities;
    using Domain.Service.ObjectCache;
    using Domain.Service.System;
    using Infrastructure.System;
    using Presentation.Core;

    public class RoleController : BaseController
    {
        IRoleRepository iRoleRepository;
        public RoleController()
        {
            iRoleRepository = new RoleRepository();
        }

        //
        // GET: /Role/

        public ActionResult Index(int? roleSelect)
        {
            var model = iRoleRepository.GetEntities();

            if (roleSelect.HasValue && roleSelect.Value == 1)
                return View("RoleSelectMore", model);
            else
                return View(model);
        }

        //
        // GET: /Role/Create

        public ActionResult Create()
        {
            return View();
        }

        //
        // POST: /Role/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                var entity = new WebManageRoles();
                TryUpdateModel(entity);
                entity.UpdateDate = DateTime.Now;
                entity.Status = 100;
                entity.Operator = string.Empty;
                iRoleRepository.Add(entity);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Role/Edit/5

        public ActionResult Edit(int id)
        {
            return View(iRoleRepository.Find(id));
        }

        //
        // POST: /Role/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                var entity = iRoleRepository.Find(id);
                TryUpdateModel(entity);
                entity.UpdateDate = DateTime.Now;
                iRoleRepository.Modify(entity);
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        public ActionResult Delete(int id)
        {
            try
            {
                iRoleRepository.Remove(new WebManageRoles { ManageRoleID = id });
                return RedirectArtDialogToAction("操作成功", "Index");
            }
            catch (Exception ex)
            {
                return RedirectArtDialogToAction("操作失败，信息为:" + ex.Message, "Index");
            }
        }
    }
}
